<?php
/*
Template Name: Modifica Preziosi
*/
session_start();
$templatePath =  get_template_directory();
include($templatePath.'/classe_resize.php');
global $wpdb;
?>
<script>
function numeralsOnly(evt) {
/*
  evt = (evt) ? evt : event;
  var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
  if (charCode > 31 && (charCode < 46 || charCode > 57)) {
    alert("Puoi inserire solo numeri e il punto decimale!");
    return false;
  }
*/
  return true;
}

function invia(cat,id){
	window.location = 'http://www.claudiahamann.com/modifica-preziosi/?id='+id+'&categoria='+cat;
}
</script>
<style type="text/css">
<!--
.Stile31 {font-size: 14px}
.Stile32 {font-size: 14px; color: #FFFFFF; }
.Stile33 {color: #FFFFFF}
.Stile36 {font-size: 14px; color: #333333; }
.Stile35 {	font-size: 16px;
	font-weight: bold;
	color: #FFFFFF;
}
-->
</style>
<?php get_header(); ?>

			<?php get_sidebar('top'); ?>
			<table width="100%" border="0" cellspacing="0" cellpadding="5">
  				<tr>
    				<td colspan="0" valign="top"><font color="#193E52" size="+2" face="Arial, Helvetica, sans-serif"><b><?php echo theme_get_meta_option($post->ID, 'theme_show_page_title') ? get_the_title() : ''?></b></font></td>
  				</tr>
			</table>
<?php
$path = WP_CONTENT_DIR."/uploads/";
function Upload_Resize($path,$immagine,$tmp_img){
	$error['flag'] = FALSE;
	$error['msg'] = "";
	$uploads = wp_upload_bits($immagine,NULL ,file_get_contents($tmp_img));
	if(!($uploads['error'] == FALSE)){ 
		//print_r($uploads);
		$error['flag'] = TRUE;
		$error['msg'] = "wp_upload_bits error<br>";
		return $error;
	}
	//echo 'file = '.$uploads['file'].'======<br>';
	//echo 'url  = '.$uploads['url']. '======<br>';
	//echo 'dest = '.$path. "lista/lista/".$immagine;
	$moveResult = rename( $uploads['file'] , $path."lista/lista/".$immagine );
	if ($moveResult == FALSE){
		$error['flag'] = TRUE;
		$error['msg'] = "rename file error";
		return $error;
	}	
	$resize = new resize;
	$resize->urlimage = $path."lista/lista/".$immagine;
	$resize->fisso = 0;
	$resize->maxX = 600;
	$resize->maxY = 600;
	$resize->latofisso = "X";
	$resize->folder = $path."lista/lista/thumbnails/";
	$resize->newName = "thumb_".$immagine;
	//
	$size = getimagesize($resize->urlimage);
	//echo '<br>$size[0]*$size[1]*4 = '.$size[0]*$size[1]*4 .'<br>';
	//echo '<br>memory_get_peak_usage()= '.memory_get_peak_usage().'<br>';
	if (($size[0]*$size[1]*4)>80000000){
			//echo "<script>alert('image to big = ".($size[0]*$size[1]*4)>8000000). "!!')</script>";
			$resize->errore[0] = 'image to big = '.($size[0]*$size[1]*4).'>80000000 !!';
		}else{
			$resize->go();
			unset($resize);


		$resize = new resize;
		$resize->urlimage = $path."lista/lista/".$immagine;
		$resize->fisso = 0;
		$resize->maxX = 600;
		$resize->maxY = 600;
		$resize->latofisso = "X";
		$resize->folder = $path."lista/lista/";
		$resize->newName = "grande_".$immagine;
		//$resize->go();
		//@unlink($path."lista/lista/".$immagine);
		$moveResult = rename( $path."lista/lista/".$immagine , $path."lista/lista/grande_".$immagine );
		}	
	//	
	if(count($resize->errore)==0){
		/*
		echo "Nome file originale: ".$resize->originalName."<br>";
		echo "Altezza file originale: ".$resize->originalY."<br>";
		echo "Larghezza file originale: ".$resize->originalX."<br>";
		echo "Nuova Altezza: ".$resize->newY."<br>";
		echo "Nuova Larghezza: ".$resize->newX."<br>";
		*/
	}else{
		$error['flag'] = TRUE;
		$error['msg'] = implode('<br>', $resize->errore);
		return $error;
	}
}

$id = $_GET['id'];
if($_POST['conferma'] == "Conferma"){
	$nome_ita=$_POST['nome_ita'];
	$nome_eng=$_POST['nome_eng'];
	$code=$_POST['code'];
	$tipo=$_POST['categoria'];
	$varietà = $_POST['sottocategoria'];
	$certificato = $_POST['certificato'];
	$carati = $_POST['carati'];
	$taglio = $_POST['taglio'];
	$forma = $_POST['forma'];
	$altezza = $_POST['altezza'];
	$lunghezza = $_POST['lunghezza'];
	$profondità = $_POST['profondità'];
	$origine = $_POST['origine'];
	$note = $_POST['note'];
	$prezzo_carato = $_POST['prezzo'];
        $currency = $_POST['currency'];
        $price_type = $_POST['price_type'];
		
	if(!empty($_FILES['datasheet']['name'])){	
	
			$datasheet_link = $_FILES['datasheet']['name'];
			$error = move_uploaded_file($_FILES['datasheet']['tmp_name'], $path."/datasheet/".$_FILES['datasheet']['name']);
		
			if ( !$error) { 
				$msg = $_FILES['datasheet']['tmp_name'].'error upload';
				echo "<script>alert('".$msg."')</script>";	
				}
			}
	
	if(!empty($_FILES['image']['name'])){
		
		$immagine = $_FILES['image']['name'];
		$caratteri = array(" ","'","(",")","[","]","/","_");
		$immagine = str_replace($caratteri,"",$immagine);
		$immagine = strtolower($immagine);	
		
		
		
		
		
		
		
		
		
		
		
		
		$error=Upload_Resize($path,$immagine,$_FILES['image']['tmp_name']);
		if ( $error['flag'] == FALSE)
			{
			
				if($wpdb->query("UPDATE {$wpdb->prefix}preziosi SET nome_ita='$nome_ita', nome_eng='$nome_eng', tipo='$tipo', varietà='$varietà', certificato='$certificato', carati='$carati', taglio='$taglio', forma='$forma', altezza='$altezza', lunghezza='$lunghezza', profondità='$profondità', origine='$origine', note='$note', prezzo='$prezzo_carato', foto='$immagine', currency='$currency',price_type='$price_type', code='$code', datasheet_link ='$datasheet_link' WHERE id='$id'")){
					$msg = "Modifica avvenuta con successo!";
				}else{
					$msg = "errore database!";
				}
			}else{
				$msg = $error['msg'];	
			}	
	echo "<script>alert('".$msg."')</script>";
	}else{
		if($wpdb->query("UPDATE {$wpdb->prefix}preziosi SET nome_ita='$nome_ita', nome_eng='$nome_eng', tipo='$tipo', varietà='$varietà', certificato='$certificato', carati='$carati', taglio='$taglio', forma='$forma', altezza='$altezza', lunghezza='$lunghezza', profondità='$profondità', origine='$origine', note='$note', prezzo='$prezzo_carato', currency='$currency',price_type='$price_type', code='$code', datasheet_link ='$datasheet_link' WHERE id='$id'"))
			echo "<script>alert('Modifica avvenuta con successo!')</script>";
		else
			echo "<script>alert('ERRORE: Modifica non riuscita!')</script>";	
	}
}
//echo $id;
$popup_sql = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}preziosi WHERE id = '$id'");
foreach($popup_sql as $popup){
	$id=$popup->id;
	$nome_ita=$popup->nome_ita;
	$nome_eng=$popup->nome_eng;
	$code=$popup->code;
	$categoria=$popup->tipo;
	$sottocategoria=$popup->varietà;
	$varietà=$wpdb->get_var("SELECT nome FROM {$wpdb->prefix}subcat_preziosi WHERE id='$sottocategoria' AND categoria='$categoria'");
	$tipo=$wpdb->get_var("SELECT nome FROM {$wpdb->prefix}cat_preziosi WHERE id= '$categoria'");	
	$certificato=$popup->certificato;
	$carati=$popup->carati;
	$taglio=$popup->taglio;
	$forma=$popup->forma;
	$altezza=$popup->altezza;
	$lunghezza=$popup->lunghezza;
	$profondità=$popup->profondità;
	$origine=$popup->origine;
	$note=$popup->note;
	$prezzo_carato=$popup->prezzo;
	$foto=$popup->foto;
	$data_sheet = $popup->datasheet_link;
?>
<form action="<?php $_SESSION['file:///Macintosh HD/Applications/MAMP/htdocs/hamann/wp-content/themes/bluconpietrepiccoleebarreaimenu/PHP_SELF']; ?>" name="form" method="post" enctype="multipart/form-data">
<table width="100%" border="0" cellpadding="0" cellspacing="0" bordercolor="#0C3062">
	<tr>
		<td valign="top" bgcolor="#FFFFFF">
	    	<table width="100%" height="362" border="0" cellpadding="2" cellspacing="1" style="border:0px">
				<tr>
					<td width="150" height="21" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid">
                    	<span class="Stile36" style="margin-left:2px">
	                    <img src="<?php echo network_home_url(); ?>/wp-content/uploads/lista/lista/thumbnails/thumb_<?php echo $foto; ?>" width="150" height="125" />
    	                </span>
					</td>
					<td valign="middle" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><input type="file" name="image" size="60"></td>
				</tr> 
				<tr>
					<td height="21" background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
                    Code</span></td>
					<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid">
                    <input type="text" name="code" value="<?php echo $code; ?>" />
                    </td>
				</tr>                 
			    <tr>
					<td height="21" background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
                    Categoria: 
                    </span></td>
					<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid">
        				<select name="categoria" id="categoria" onchange="invia(this.value,<?php echo $_GET['id']; ?>)" >
			            <?php
							$categorie_sql = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}cat_preziosi ORDER BY id");
							foreach ($categorie_sql as $categorie){
								if( (isset($_GET['categoria'])) ){
									if($categorie->id==$_GET['categoria']){
										echo "<option value=".$categorie->id." selected=\"selected\">".ucfirst($categorie->nome)."</option>";
									}else{
										echo "<option value=".$categorie->id.">".ucfirst($categorie->nome)."</option>";
								}
							}else{
								if($categorie->id == $categoria){
									echo "<option value=".$categorie->id." selected=\"selected\">".ucfirst($categorie->nome)."</option>";
								}else{
									echo "<option value=".$categorie->id.">".ucfirst($categorie->nome)."</option>";
								}
							}
						}
						?>
						</select>
	        		</td>
				</tr>
				<tr>
					<td height="21" background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
                    Sottocategoria: </span></td>
					<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
						<select name="sottocategoria">
							<?php
								if(isset($_GET['categoria'])){
									$categorie=$_GET['categoria'];
									$subcat_sql = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}subcat_preziosi WHERE categoria = '$categorie' ORDER BY id");
								}else
									$subcat_sql = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}subcat_preziosi WHERE categoria = '$categoria' ORDER BY id");
						
								foreach($subcat_sql as $subcat){
									if($sottocategoria == $subcat->id)
										echo "<option value=".$subcat->id." selected=\"selected\">".ucfirst($subcat->nome)."</option>";
									else
										echo "<option value=".$subcat->id.">".ucfirst($subcat->nome)."</option>";
								}
							?>
						</select>
						</span></td>
				</tr>  
		    	<!--tr>
					<td bgcolor="#174E9B"><span class="Stile32" style="margin-left:5px">Nome Italiano</span></td>
					<td bgcolor="#174E9B"><input type="text" name="nome_ita" value="<?php //echo $nome_ita; ?>" /></td>
				</tr>
				<tr>
					<td bgcolor="#247CD7"><span class="Stile32" style="margin-left:5px">Nome Inglese</span></td>
					<td bgcolor="#247CD7"><input type="text" name="nome_eng" value="<?php //echo $nome_eng; ?>" /></td>
				</tr>
				<tr>
					<td bgcolor="#174E9B"><span class="Stile32" style="margin-left:5px">Tipo</span></td>
					<td bgcolor="#174E9B"><input type="text" name="tipo" value="<?php //echo $tipo; ?>" /></td>
				</tr>
				<tr>
					<td bgcolor="#247CD7"><span class="Stile32" style="margin-left:5px">Varietà</span></td>
					<td bgcolor="#247CD7"><input type="text" name="varietà" value="<?php //echo $varietà; ?>" /></td>
				</tr-->
				<tr>
					<td height="21" background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
                    Certificato</span></td>
					<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid">
        				<input type="radio" name="certificato" value="yes" <?php if($certificato=="si") echo "checked=\"checked\"";?>/>
                        <span class="Stile33"><span class="Stile36" style="margin-left:2px">SI </span>
		    	    	<input type="radio" name="certificato" value="no" <?php if($certificato=="no") echo "checked=\"checked\"";?>/>
                        <span class="Stile36" style="margin-left:2px">NO</span></span>
	        		</td>
				</tr>
				<tr>
					<td height="21" background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
                    Carati</span></td>
					<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid">
                    <input type="text" name="carati" value="<?php echo $carati." cts"; ?>" onkeypress="return numeralsOnly(event)"/>
                    </td>
				</tr>
				<tr>
					<td height="21" background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid">
                    <span class="Stile36" style="margin-left:2px">Taglio</span></td>
					<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid">
                    <input type="text" name="taglio" value="<?php echo $taglio; ?>" />
                    </td>
				</tr>
				<tr>
					<td height="21" background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
                    Forma</span></td>
					<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid">
                    <input type="text" name="forma" value="<?php echo $forma; ?>" />
                    </td>
				</tr>
				<tr>
					<td height="21" background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
                    Dimensioni</span></td>
					<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid">
						<table>
							<tr>
								<td width="65"><input type="text" name="altezza" value="<?php echo $altezza; ?>" onkeypress="return numeralsOnly(event)"/></td>
                                <td><span class="Stile36" style="margin-left:2px">X</span></td>
								<td width="79"><input type="text" name="lunghezza" value="<?php echo $lunghezza; ?>" onkeypress="return numeralsOnly(event)"/></td>
                                <td><span class="Stile36" style="margin-left:2px">X</span></td>
								<td width="117"><input type="text" name="profondità" value="<?php echo $profondità; ?>" onkeypress="return numeralsOnly(event)"/></td>
                                <td><span class="Stile36" style="margin-left:2px">mm</span></td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<td height="24" valign="top" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
                    Origine</span></td>
					<td style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
                    <input type="text" name="origine" value="<?php echo $origine; ?>" /></td>
				</tr>              
				<tr>
					<td height="24" valign="top" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
                    Note</span></td>
					<td style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
                    <textarea name="note" rows="5" cols="71"><?php echo $note; ?></textarea></td>
				</tr>  
				<tr>
					<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">foglio caratteristiche</span></td>
					<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
						<input type="file" name="datasheet" accept=".pdf,.doc,.docx" value="<?php echo $data_sheet?>" /> 
						foglio caratteristiche in uso : <?php echo $data_sheet?>
						</span>
					</td>
				</tr>    
				
				<tr>
					<td height="24" bgcolor="#174E9B">
                                            <!--span class="Stile35" style="margin-left:3px">Prezzo per carato</span-->
                                            <select name="price_type">
                                                <option value="cad">Per carato</option>
                                                <option value="tot">Totale</option>
                                            </select>
                                        </td>
					<td bgcolor="#174E9B"><span class="Stile35" style="margin-left:3px">
                    <input type="text" name="prezzo" value="<?php if($prezzo_carato!=NULL) echo $prezzo_carato; else echo "-"; ?>" onkeypress="return numeralsOnly(event)" style="float:left; width: 550px;"/>
                    <select id="currency" name ="currency"><option value="€">€</option><option  value="$">$</option></select>
                    </span></td>
				</tr>  
			</table>
		</td>
	</tr>
</table>
<?php
}
if(!isset($_POST['conferma'])){
?>
<input name="reset" type="reset" id="reset" onClick="history.back()" value="Indietro" nome="reset">
<?php
}elseif($_POST['categoria'] == 1){
	$pagina = "/catalogo-pietre/";
	echo "<input name=\"reset\" type=\"reset\" id=\"reset\" onClick=\"location.href='".get_home_url().$pagina."'\" value=\"Indietro\" nome=\"reset\">";
}elseif($_POST['categoria'] == 3){
	$pagina = "";
	echo "<input name=\"reset\" type=\"reset\" id=\"reset\" onClick=\"location.href='".get_home_url().$pagina."'\" value=\"Indietro\" nome=\"reset\">";
}else{
	$pagina = "/lista-gemme-double-rose/";
	echo "<input name=\"reset\" type=\"reset\" id=\"reset\" onClick=\"location.href='".get_home_url().$pagina."'\" value=\"Indietro\" nome=\"reset\">";
}
?>
<input type="submit" name="conferma" value="Conferma">
</form>

				<?php get_sidebar('bottom'); ?>

<?php get_footer(); ?>